import requests
from lxml import etree
import re
import pymysql
#字母列表

def Zi_list():
    url = 'http://10.60.10.34:8080/mobile/mobile/category'
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36"
    }
    r =requests.get(url,headers=headers)
    html = etree.HTML(r.text)
    urls = html.xpath('/html/body/div/div/ul/li/ul/li/a/@href')
    return urls
#分页list
def Fen_list():
    for i in Zi_list():
        url = 'http://10.60.10.34:8080'+i
        r =requests.get(url)
        html = etree.HTML(r.text)
        urls = html.xpath('//*[@id="page"]/span/text()')
        num = re.findall("(\d+)",urls[0])
        if int(num[0]) <= 15:
            Url = url+'&pager.offset=0'
            get_y(Url)
        else:
            Num = int(num[0])/15
            x = 0
            for i in range(0,int(Num) + 1):
                x = x+15
            # Url = url + '&pager.offset='+str()
                Url = url+'&pager.offset='+str(x)
                get_y(Url)

def get_y(url):
    r = requests.get(url)
    html = etree.HTML(r.text)
    name = html.xpath('//*[@id="detailTable"]/ul/li/a/h2/text()') #书名
    chu = html.xpath('//*[@id="detailTable"]/ul/li/a/div/p[5]/text()')#出版社
    zuozhe = html.xpath('//*[@id="detailTable"]/ul/li/a/div/div/text()')#作者
    urls = html.xpath('//*[@id="detailTable"]/ul/li/a/@href') #url
    chus = []
    for i in chu:
        if i !='\r\n\t\t\t\t\t\t\t\t\t\t\t':
            chus.append(i)
    for q,w,e,r in zip(name,chus,zuozhe,urls):
        Mysql_etid(q,w,e,r)
        print(q,w,e,r)

def Mysql_etid(name,chu,zuozhe,urls):
    cur.execute('insert into tu (name,chu,zuozhe,urls) values(%s,%s,%s,%s)',[name,chu,zuozhe,urls])
    con.commit()

if __name__ == '__main__':
    con = pymysql.connect(host='10.30.59.136', port=3306, user='root', password='123456', database='wdr',
                          charset='utf8')
    cur = con.cursor()
    Fen_list()